package org.dce.screencapturer.objects;

import java.awt.BasicStroke;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;

import org.dce.screencapturer.actions.drawing.InsertObjectAction;
import org.dce.screencapturer.util.IconFactory;

@SuppressWarnings("serial")
public class Banner extends RegularShapeObject {
	
	public static final class InsertAction extends InsertObjectAction {
		public InsertAction(){
			super("actions.banner",IconFactory.getIcon("/images/text.png"), null,Banner.class);
		}
	};

	public Banner(){
		setText("Write text here");
		getLineAttributes().setStroke(new BasicStroke(1));
		getTextAttributes().setFont(new Font("Dialog",Font.BOLD,22));
	}

	protected Rectangle calculateInnerBounds(Shape shape) {
		return new Rectangle(0,0,getWidth(),getHeight());
	}
	
	@Override
	protected Shape getShape() {
		
		FontRenderContext context = ((Graphics2D)getGraphics()).getFontRenderContext();
		
		TextLayout textLayout = new TextLayout(getText(), getTextAttributes().getFont(), context);
		
		AffineTransform transform = new AffineTransform();
		transform.translate(0, textLayout.getBounds().getHeight());
		
		return textLayout.getOutline(transform);
	}
	
	protected boolean supportsText(){
		return false; // The shape is the text by its own.
	}

	@Override
	public void setShapeAttributes(ShapeAttributes shapeAttributes) {
		rebuildShape();
		super.setShapeAttributes(shapeAttributes);
	}

	@Override
	public void setTextAttributes(TextAttributes textAttributes) {
		rebuildShape();
		super.setTextAttributes(textAttributes);
	}
}
